// sender.cpp : Defines the entry point for the console application.
//

#ifdef _MSC_VER
#include "stdafx.h"
#endif

 /*
 * sender.c -- multicasts "hello, world!" to a multicast group once a second
 *
 * Antony Courtney,	25/11/94
 */
#include <Utility.h>
#include <assert.h>





#define HELLO_PORT 20000
#define HELLO_GROUP "225.0.0.37"
#define MSGBUFSIZE 1024*64*2


int main(int argc, char *argv[])
{
     struct sockaddr_in addr;
     int fd;
	 u_char ttl = 0;
	 u_char loop = 0;
     char message[MSGBUFSIZE]={0};

     FIX::socket_init();
     
	 /* create what looks like an ordinary UDP socket */
     if ((fd=socket(AF_INET,SOCK_DGRAM,IPPROTO_IP)) < 0) {
	  perror("socket");
	  exit(1);
     }

	      /* set up destination address */
     memset(&addr,0,sizeof(addr));
     addr.sin_family=AF_INET;
     addr.sin_addr.s_addr=inet_addr(HELLO_GROUP);
     addr.sin_port=htons(HELLO_PORT);
     

	 
	 loop = 1;
	 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, (const char*)&loop, sizeof(loop)) < 0 )
	 {
		 perror("setsockopt-IP_MULTICAST_LOOP");
		 exit(1);
	 }
	 
	 
	 ttl = 1;
	 //Note that ttl is an unsigned char. Any of the values 0 through 255 can be specified. 
	 // If ttl is zero, the multicast is limited to the local system. 
	 // If ttl is one, the multicast is limited to a local network. 
	 // If ttl is two, the multicast can forwarded through at most one gateway; and so forth.

#ifdef _MSC_VER
	 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, (char FAR*)&ttl, sizeof(ttl) ) < 0)
#else
	 if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl) ) < 0)
#endif
	 {
		 perror("setsockopt-IP_MULTICAST_TTL");
		 exit(1);
	 }

     /* now just sendto() our destination! */
	 unsigned int count = 0;
	 //C:\Production_NymexDataServer\NymexDataServer\NymexDataServer\bin\Release
	 //FILE* fp = fopen("C:\\Test_NymexDataServer\\NymexDataServer\\NymexDataServer\\bin\\Release\\FIXT.1.1_FIX_USER1_FIXUDP-MD_FEED_A_Original.logb", "rb");
	 //FILE* fp = fopen("C:\\BinaryLogFiles\\FIXT.1.1_FIX_USER1_FIXUDP-MD_FEED_A_Original.logb", "rb");
	 FILE* fp = fopen(argv[1], "rb");
	 
	 if ( !fp )
	 {
		 perror("HexFile Not Found");
		 return -1;
	 }
     while (1) {
		 //FILE* fp = fopen("C:\\Users\\nsharma\\Desktop\\Xcmeinput.data", "rb");
		 memset(&message[0], 0, sizeof(message));
		 int size = fread(message, 1, 4, fp );
		 count++;
		 if ( count %  1000 == 0)
			 printf(" sent %d messages\n", count );

		 if (size == 0 ) 
		 {
			 printf("Done!%d\n", count);
			 exit(0);
		 }

		 if(count == 2)
		 {
			 printf("Done!%d\n", count);
			 exit(0);
		 }

		 assert( size == 4);
		 size = *((int*)message);
		 sprintf( message, "%05d", count);
		 size = fread(message+5, 1, size, fp ) + 5;
		 int iret = sendto(fd,message,size,0,(struct sockaddr *) &addr, sizeof(addr));
		 assert( size == iret);
		 assert ( size != -1 );
		 //if (iret != size)
		 //{
			// if ( iret != -1) 
			// {
				 if ( iret < 0 || iret != size) 
				 {
				   perror("sendto");
				  
				 }
			//	 printf("could not send %d bytes", size-iret );
			// }
			// 
		 //}
		 //printf(" send %d bytes\n", count++ );
		 if ( count % 3000 == 0)
			 FIX::process_sleep(.01);
     }
	 fclose(fp);
}
